package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.SensorWarnController;
import com.antoco.operation.bigscreen.model.dto.SensorWarnDTO;
import com.antoco.operation.bigscreen.model.dto.SensorWarnShowDTO;
import com.antoco.operation.bigscreen.model.entity.Sensor;
import com.antoco.operation.bigscreen.model.entity.SensorWarn;
import com.antoco.operation.bigscreen.mapper.SensorWarnMapper;
import com.antoco.operation.bigscreen.mapper.SensorWarnShowMapper;
import com.antoco.operation.bigscreen.service.WebSocketServer;
import com.antoco.operation.bigscreen.service.autogeneration.impl.SensorWarnServiceImpl;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorWarnService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/sensor-warn")
@RestController
public class SensorWarnControllerImpl implements SensorWarnController {

    @Autowired
    private SensorWarnService sensorWarnService;

    @Autowired
    private SensorWarnMapper sensorWarnMapper;

    @Autowired
    private SensorWarnShowMapper sensorWarnShowMapper;

    @Autowired
    private SensorService sensorService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public SensorWarnDTO save(@RequestBody SensorWarnDTO sensorWarnDTO){
        try {
            SensorWarn sensorWarn = sensorWarnMapper.asEntity(sensorWarnDTO);
            Sensor sensor = sensorService.findById(sensorWarnDTO.getSensorId()).orElse(null);
            sensorWarn.setSensor(sensor);
            sensorWarn.setGenTime(new Timestamp(System.currentTimeMillis()));
            sensorWarnService.save(sensorWarn);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sensorWarnDTO;
    }

    @Override
    @GetMapping("/{id}")
    public SensorWarnDTO findById(@PathVariable("id") String id) {
        SensorWarn sensorWarn = sensorWarnService.findById(id).orElse(null);
        return sensorWarnMapper.asDTO(sensorWarn);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        sensorWarnService.deleteById(id);
    }

    @Override
    @GetMapping
    public List<SensorWarnShowDTO> list() {
        return sensorWarnShowMapper.asDTOList(sensorWarnService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<SensorWarnDTO> pageQuery(Pageable pageable) {
        Page<SensorWarn> sensorWarnPage = sensorWarnService.findAll(pageable);
        List<SensorWarnDTO> dtoList = sensorWarnPage
                .stream()
                .map(sensorWarnMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, sensorWarnPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public SensorWarnDTO update(@RequestBody SensorWarnDTO sensorWarnDTO, @PathVariable("id") String id) {
        SensorWarn sensorWarn = sensorWarnMapper.asEntity(sensorWarnDTO);
        return sensorWarnMapper.asDTO(sensorWarnService.update(sensorWarn, id));
    }
}